
<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>定制商品订单管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item style="width: 400px">
          <el-col :span="7">
            <el-select
              v-model="queryInfo.query.time_type"
              placeholder="请选择"
              size="mini"
            >
              <el-option label="创建日期" value="1"></el-option>
              <el-option label="付款日期" value="2"></el-option>
              <el-option label="完成日期" value="3"></el-option>
            </el-select>
          </el-col>
          <el-col :span="8">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.start_time"
              size="mini"
              style="width: 100%"
            ></el-date-picker>
          </el-col>
          <el-col class="line" :span="1">-</el-col>
          <el-col :span="8">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.end_time"
              size="mini"
              style="width: 100%"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="商品名称" prop="goods_id">
          <el-select
            v-model="queryInfo.query.goods_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.goods"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryInfo.query.status"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option key="11" label="待付款" value="11"> </el-option>
            <el-option key="12" label="已支付" value="12"> </el-option>
            <el-option key="13" label="支付中" value="13"> </el-option>
            <el-option key="14" label="已发货" value="14"> </el-option>
            <el-option key="49" label="已签收" value="49"> </el-option>
            <el-option key="50" label="已完成" value="50"> </el-option>
            <el-option key="60" label="已取消" value="60"> </el-option>
            <el-option key="69" label="退款中" value="69"> </el-option>
            <el-option key="70" label="已退款" value="70"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款方式" prop="pay_way">
          <el-select
            v-model="queryInfo.query.pay_way"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option key="1" label="微信" value="1"> </el-option>
            <el-option key="2" label="支付宝" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字" prop="key">
          <el-input
            v-model="queryInfo.query.key"
            placeholder="请输入订单编号或手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
        >
          导出数据
        </el-button>
      </div>

      <!-- 用户列表区域 -->
      <el-table :data="list" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="订单信息" min-width="150px" max-width="300px">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="toDetail(scope.row.id, scope.row.express_num)"
              >{{ scope.row.code }}</el-link
            >
            <br />
            <span> {{ scope.row.create_time }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户信息" min-width="100px">
          <template slot-scope="scope">
            <span> {{ scope.row.create_user_name }}</span>
            <br />
            <span> {{ scope.row.create_user_phone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品信息" min-width="200px" max-width="300px">
          <template slot-scope="scope">
            <span> {{ scope.row.goods_name }}【{{ scope.row.goods_id }}】</span>
            <br />
            <span class="greyTitle">商品数量：</span>
            <span> {{ scope.row.goods_num }}</span>
            <br />
            <span class="greyTitle"
              >商品单价： {{ scope.row.goods_price }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="商品金额" min-width="120px">
          <template slot-scope="scope">
            <span class="greyTitle">应付金额： </span>
            <span>{{ scope.row.price }}</span>
            <br />
            <span class="greyTitle">商品总额： </span>
            <span>{{ scope.row.goods_total_price }}</span>
          </template>
        </el-table-column>
        <el-table-column label="优惠券抵扣">
          <template slot-scope="scope">
            <span> {{ scope.row.discount_amount }}</span>
            <br />
            <span> {{ scope.row.coupon_code }}</span>
          </template>
        </el-table-column>
        <el-table-column label="付款信息" min-width="150px" max-width="300px">
          <template slot-scope="scope">
            <span> {{ scope.row.pay_way.name }}</span>
            <br />
            <span> {{ scope.row.pay_finish_time }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status.name"></el-table-column>
        <el-table-column
          label="备注"
          prop="stander"
          min-width="150px"
          max-width="300px"
        >
          <template slot-scope="scope">
            <span class="greyTitle"> 客户备注：</span>
            <span>{{ scope.row.customer_remark }}</span>
            <br />
            <span class="greyTitle"> 后台备注：</span>
            <span>{{ scope.row.plat_remark_end }}</span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var date = new Date()
    return {
      queryInfo: {
        query: {
          time_type: '1',
          start_time: '',
          end_time: '',
          goods_id: '',
          status: '',
          pay_way: '',
          key: ''
        },
        pagenum: 1,
        pagesize: 20,
        key: ''
      },
      selectData: [],
      list: [],
      total: 0,
      btnLoading: false
    }
  },
  created() {
    this.getSelectData()
    this.initDate()
    this.getListData()
  },
  methods: {
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'goods'
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    initDate() {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      this.queryInfo.query.start_time = this.getFormatDate(start)
      this.queryInfo.query.end_time = this.getFormatDate(end)
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/plate_order/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    toDetail(id, expressNum) {
      const newpage = this.$router.resolve({
        path: '/commodity/AntiLostCardDetail',
        query: { id: id, expressNum: expressNum }
      })
      window.open(newpage.href, '_blank')
    },
    downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/plate_order/export',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '商品订单.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          this.$message.error(err)
        })
    }
  }
}
</script>

<style lang="less" scoped>
.line {
  text-align: center;
}
.greyTitle {
  color: #999;
}
</style>
